﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Forms.Integration;

namespace ControlLibary
{
    public class ComboBoxColumn : DataGridViewColumn
    {

        public ComboBoxColumn() : base(new ComboBoxCell())
        {
        }

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                // Ensure that the cell used for the template is a ComboBoxCell.
                if (value != null &&
                    !value.GetType().IsAssignableFrom(typeof(ComboBoxCell)))
                {
                    throw new InvalidCastException("Must be a ComboBoxCell");
                }
                base.CellTemplate = value;
            }
        }

        private List<object> itemsSources;

        public List<object> ItemsSources
        {
            get { return itemsSources; }
            set { itemsSources = value; }
        }
    }

    public class ComboBoxCell : DataGridViewTextBoxCell
    {
        public ComboBoxCell()
            : base()
        {

            this.Style.Format = "d";
        }

        public override void InitializeEditingControl(int rowIndex, object
            initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            // Set the value of the editing control to the current cell value.
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);

            ComboBoxEditingControl cc = DataGridView.EditingControl as ComboBoxEditingControl;
            // Use the default row value when Value property is null.
            if (this.Value != null)
            {
                cc.Value = this.Value as string;
            }
            else
            {
                cc.Value = "";
            }

            List<object> objs = (this.OwningColumn as ComboBoxColumn).ItemsSources;

            if (objs != null)
                cc.Source = objs;

            cc.Focus();
        }

        public override Type EditType
        {
            get
            {
                return typeof(ComboBoxEditingControl);
            }
        }

        public override Type ValueType
        {
            get
            {
                return typeof(string);
            }
        }

        public override object DefaultNewRowValue
        {
            get
            {
                return "";
            }
        }
    }

    class ComboBoxEditingControl : ElementHost, IDataGridViewEditingControl
    {
        private System.Windows.Controls.ComboBox com { get; set; }

        DataGridView dataGridView;
        private bool valueChanged = false;
        int rowIndex;

        public ComboBoxEditingControl()
        {
            this.com = new System.Windows.Controls.ComboBox();
            this.com.Margin = new System.Windows.Thickness(0);
            this.Child = this.com;
            this.com.IsEditable = true;
            this.com.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
            this.com.SelectionChanged += Com_SelectionChanged;
        }

        public new void Focus()
        {

        }

        private void Com_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            valueChanged = true;
            this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
        }

        public object EditingControlFormattedValue
        {
            get
            {
                return this.com.Text;
            }
            set
            {
                if (value is String)
                {
                    try
                    {
                        this.com.Text = value as string;
                    }
                    catch
                    {
                        this.com.Text = "";
                    }
                }
            }
        }

        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue;
        }

        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
        }

        public int EditingControlRowIndex
        {
            get
            {
                return rowIndex;
            }
            set
            {
                rowIndex = value;
            }
        }

        public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
        {
            // Let the DateTimePicker handle the keys listed.
            switch (key & Keys.KeyCode)
            {
                case Keys.Left:
                case Keys.Up:
                case Keys.Down:
                case Keys.Right:
                case Keys.Home:
                case Keys.End:
                case Keys.PageDown:
                case Keys.PageUp:
                    return true;
                default:
                    return !dataGridViewWantsInputKey;
            }
        }

        public void PrepareEditingControlForEdit(bool selectAll)
        {
            // No preparation needs to be done.
        }

        public bool RepositionEditingControlOnValueChange
        {
            get
            {
                return false;
            }
        }

        public DataGridView EditingControlDataGridView
        {
            get
            {
                return dataGridView;
            }
            set
            {
                dataGridView = value;
            }
        }

        public bool EditingControlValueChanged
        {
            get
            {
                return valueChanged;
            }
            set
            {
                valueChanged = value;
            }
        }

        public Cursor EditingPanelCursor
        {
            get
            {
                return base.Cursor;
            }
        }

        public object Value
        {
            get { return com.Text; }
            set
            {
                com.Text = value as string;
                com.SelectedItem = value as string;
            }
        }

        public List<object> Source
        {
            set
            {
                if (this.com.ItemsSource != null)
                    this.com.ItemsSource = null;
                this.com.ItemsSource = value;
            }
        }
    }
}
